package com.irdstudio.efp.esb.api.rest.zzd;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.irdstudio.basic.framework.core.exception.BizException;
import com.irdstudio.basic.framework.core.util.DateUtility;
import com.irdstudio.basic.framework.core.util.MapBeanUtil;
import com.irdstudio.basic.framework.core.util.MessageRuleCheckUtil;
import com.irdstudio.basic.framework.core.util.StringUtil;
import com.irdstudio.efp.esb.api.bean.nls.ReqLoanAppBean;
import com.irdstudio.efp.esb.api.bean.nls.RespLoanAppBean;
import com.irdstudio.efp.esb.api.service.TransferService;
import com.irdstudio.efp.esb.common.constant.EsbBizEnums;
import com.irdstudio.efp.nls.service.facade.NlsApplyInfoService;
import com.irdstudio.efp.nls.service.vo.NlsApplyInfoVO;

/**
 * 用户借款请求接口逻辑处理类
 * 
 * @author huangwb
 * @since 2018年12月18日 下午5:54:28
 * @version 1.0
 */
@Service("loanApplyController")
public class LoanApplyController implements TransferService {

	private static Logger logger = LoggerFactory.getLogger(LoanApplyController.class);

	@Autowired
	@Qualifier("nlsApplyInfoService")
	private NlsApplyInfoService nlsApplyInfoService;

	/**
	 * 用户借款请求接口逻辑处理方法，处理逻辑包括：<br>
	 * 1.将用户借款请求报文体对象转换成用户借款请求对应的domain；<br>
	 * 2.对用户借款请求domain中值做是否必输性校验；<br>
	 * 3.对用户借款请求domain中值做是否合规性校验；<br>
	 * 4.调用对应的中台服务；<br>
	 * 
	 * @param esbReqBodyBean
	 * @return
	 * @throws Exception
	 * @author wangyk3
	 * @since 2018年12月18日 下午5:16:37
	 * @version 1.0
	 */
	@Override
	public RespLoanAppBean initNlsApplyInfo(ReqLoanAppBean reqLoanAppBean) throws Exception {
		RespLoanAppBean respLoanAppBean = new RespLoanAppBean();
		NlsApplyInfoVO nlsApplyInfoVO = new NlsApplyInfoVO();
		Map<String, Object> reqLoanAppMap = new HashMap<String, Object>();
		String applyNo = "";
		try {
			// 1.获取交易流水号
			applyNo = reqLoanAppBean.getAppFlowNo();
			logger.info("交易流水号[" + applyNo + "]周转贷进件申请处理开始!");
			reqLoanAppMap = MapBeanUtil.beanToMap(reqLoanAppBean);

			// 2.必输项校验
			// 2.1.接口字段必输项校验
			this.checkMessageRequired(reqLoanAppMap);
			// 2.2.字段值合规性校验
			MessageRuleCheckUtil.checkDouble(reqLoanAppBean.getImpawnLot(), "质押份额");
			MessageRuleCheckUtil.checkDouble(reqLoanAppBean.getImpawnAmt(), "质押金额");
			MessageRuleCheckUtil.checkDouble(reqLoanAppBean.getLoanAmt(), "贷款金额");

			// 3.调用中台服务落表处理
			nlsApplyInfoVO.setApplySeq(applyNo);// 流水号
			nlsApplyInfoVO.setCusId("");// 客户号
			nlsApplyInfoVO.setCusName(reqLoanAppBean.getBrwNm());// 客户名称
			nlsApplyInfoVO.setCertType(reqLoanAppBean.getLoanAmt());// 证件类型
			nlsApplyInfoVO.setCertCode(reqLoanAppBean.getCertNo());// 证件号码
			nlsApplyInfoVO.setIndivIdExpDt(reqLoanAppBean.getCertMatureDt());// 证件到期日
			nlsApplyInfoVO.setApplyDate(DateUtility.getCurrAppDateStr());// 申请日期
			nlsApplyInfoVO.setApplyTime(reqLoanAppBean.getLoanAppTm());// 申请时间
			nlsApplyInfoVO.setApplyAmt(new BigDecimal(reqLoanAppBean.getLoanAmt()));// 申请金额
			nlsApplyInfoVO.setNlsApplyState("000");// 网贷申请状态 初始化审批中 TODO 常量化
			nlsApplyInfoVO.setRefuseCause("");// 拒绝原因
			nlsApplyInfoVO.setLoanTerm(reqLoanAppBean.getLoanTrm());// 贷款期限
			nlsApplyInfoVO.setLoanTermType(reqLoanAppBean.getLoanTrmType());// 期限类型
			nlsApplyInfoVO.setLoanPurpose(reqLoanAppBean.getBrwUsg());// 贷款用途
			nlsApplyInfoVO.setNlsOperUserid("");// 网贷经办人
			nlsApplyInfoVO.setNlsOperOrgid("");// 网贷经办机构
			nlsApplyInfoVO.setPrdCode(reqLoanAppBean.getProdCd());// 产品代码
			nlsApplyInfoVO.setPrdName(reqLoanAppBean.getLoanProdNm());// 产品名称
			nlsApplyInfoVO.setLoanCcy(reqLoanAppBean.getCurrCd());// 贷款币种
			nlsApplyInfoVO.setLoanRate(new BigDecimal(reqLoanAppBean.getLoanRate()));// 贷款利率
			nlsApplyInfoVO.setPledgeShares(new BigDecimal(reqLoanAppBean.getImpawnLot()));// 质押股份
			nlsApplyInfoVO.setPledgeCost(new BigDecimal(reqLoanAppBean.getImpawnAmt()));// 质押金额
			nlsApplyInfoVO.setRepayMethod(reqLoanAppBean.getRpyMd());// 还款方式
			nlsApplyInfoVO.setLoanContVersion(reqLoanAppBean.getBrwContrVerNo());// 借款合同版本号
			nlsApplyInfoVO.setPbocAuthLetter(reqLoanAppBean.getCrdtAuthBookNm());// 征信授权书
			nlsApplyInfoVO.setLoanDueDate(reqLoanAppBean.getLoanMatureDt());// 贷款到期日
			nlsApplyInfoVO.setTencentUserId("");// 腾讯用户标识
			nlsApplyInfoVO.setTencentCreditScore(new BigDecimal(reqLoanAppBean.getTCCrdtScr()));// 腾讯信用分
			nlsApplyInfoVO.setTencentNotifyUrl(reqLoanAppBean.getBackAdvInf());// 腾讯回调通知
			nlsApplyInfoVO.setRemarks(reqLoanAppBean.getRsnRmk());// 备注
			nlsApplyInfoVO.setCusGender(reqLoanAppBean.getBrwGnd());// 客户性别
			nlsApplyInfoVO.setCusNational(reqLoanAppBean.getNation());// 客户国籍
			nlsApplyInfoVO.setIndivRsdAddr(reqLoanAppBean.getCertAddr());// 居住地址
			nlsApplyInfoVO.setRecvAcctNo(reqLoanAppBean.getBankCardNo());// 放款账号
			nlsApplyInfoVO.setRecvBankName(reqLoanAppBean.getBankNm());// 放款银行名称
			nlsApplyInfoVO.setRecvBankId(reqLoanAppBean.getBankNo());// 放款银行行号
			nlsApplyInfoVO.setCreateTime(DateUtility.getCurrAppDateTimeStr());// 创建时间
			nlsApplyInfoVO.setLastModifyTime(DateUtility.getCurrAppDateTimeStr());// 最近修改时间

			nlsApplyInfoService.insertNlsApplyInfo(nlsApplyInfoVO);
			// 4 .异步调用节点 TODO

			// 5.组装返回报文
			respLoanAppBean.setRetStCd(EsbBizEnums.RetCodeEnum.SUCCESS.toString());
			respLoanAppBean.setRtnMsg("成功");
			respLoanAppBean.setBankInWrongCd("");
			respLoanAppBean.setBankInWrongRsn("");
			respLoanAppBean.setSynDistrFlg("");
			respLoanAppBean.setLoanOrdrNo(applyNo);
			respLoanAppBean.setBankCustNo("");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		} finally {
			// TODO
		}
		return respLoanAppBean;
	}

	/**
	 * @description:校验必输项
	 * @param reqBodyMap
	 * @throws BizException
	 * @date:2018年12月21日 上午10:21:13
	 * @author:wangyk3
	 */
	public void checkMessageRequired(Map<String, Object> reqBodyMap) throws BizException {
		Map<String, String> requiredMap = new HashMap<String, String>();
		requiredMap.put("applSeq", "申请流水号");
		requiredMap.put("cusName", "姓名");
		requiredMap.put("certType", "证件类型");
		requiredMap.put("certNo", "证件号码");
		requiredMap.put("mobile", "手机号码");
		requiredMap.put("prdCde", "产品代码");
		requiredMap.put("prdName", "产品名称");
		requiredMap.put("lctProductCorpSpid", "基金公司商户号");
		requiredMap.put("lctPledgeeFundCde", "基金代码");
		requiredMap.put("lctPrdCde", "理财通产品代码");
		requiredMap.put("lctPledgeShare", "质押份额");
		requiredMap.put("lctPledgeAmt", "质押金额");
		requiredMap.put("loanAmt", "贷款金额");
		requiredMap.put("loanTerm", "贷款期限");
		requiredMap.put("loanTermType", "期限类型");
		requiredMap.put("lctUserId", "真身id");
		requiredMap.put("curType", "币种");
		requiredMap.put("loanRate", "贷款利率");
		requiredMap.put("loanDueDay", "贷款到期日");
		requiredMap.put("loanReqTime", "贷款申请时间");
		requiredMap.put("repayMode", "还款方式");
		requiredMap.put("creditScoreTime", "授权征信查询时间");
		requiredMap.put("lctSignType", "签名方式");
		requiredMap.put("lctInputCharset", "字符集");
		requiredMap.put("lctSign", "签名");
		requiredMap.put("lctSignKeyIndex", "密钥序号");

		StringUtil.checkRequiredValue(reqBodyMap, requiredMap);
	}

	public NlsApplyInfoService getNlsApplyInfoService() {
		return nlsApplyInfoService;
	}

	public void setNlsApplyInfoService(NlsApplyInfoService nlsApplyInfoService) {
		this.nlsApplyInfoService = nlsApplyInfoService;
	}

}
